package com.barbarossa.activity.widget;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.androidquery.AQuery;
import com.barbarossa.activity.R;
import com.barbarossa.vod.datatype.TorrentFile;
import com.barbarossa.vod.datatype.VideoLink;
import com.barbarossa.vod.datatype.VodHistoryRecord;

public class VodHistoryListAdapter extends BaseExpandableListAdapter {

	protected Context mContext = null;
	protected ArrayList<VodHistoryRecord> vodHistory = null;

	public VodHistoryListAdapter(Context context) {
		mContext = context;
	}
	
	public void addHistoryRecord(VodHistoryRecord record) {
		vodHistory.add(0, record);
		this.notifyDataSetChanged();
	}
	
	public void clearHistoryRecord() {
		vodHistory.clear();
		this.notifyDataSetChanged();		
	}

	public ArrayList<VodHistoryRecord> getVodHistory() {
		return vodHistory;
	}

	public void setVodHistory(ArrayList<VodHistoryRecord> vodHistory) {
		this.vodHistory = vodHistory;
	}

	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return vodHistory.get(groupPosition).getTorrentRecord().getLinks()[childPosition];
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return groupPosition<<4+childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item, null);
			AQuery aq = new AQuery(convertView);
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_video);
			if(null != vodHistory.get(groupPosition).getLinkRecord()) {
				aq.id(R.id.textViewLarge).text("Error");
				aq.id(R.id.textViewSmall).text("why here link record exists?");
			} 
			else {
				TorrentFile tf = vodHistory.get(groupPosition).getTorrentRecord();				
				aq.id(R.id.textViewLarge).text(tf.getLinks()[childPosition].getName());
				aq.id(R.id.textViewSmall).text(tf.getLinks()[childPosition].getIndex() + "::" + tf.getLinks()[childPosition].getM3u8Link().length);
				switch(tf.getLinks()[childPosition].getM3u8Link().length) {
				case 1:
					aq.id(R.id.textViewSmall).text(tf.getLinks()[childPosition].getIndex()+"::"+mContext.getText(R.string.clarify_sd));
					break;
				case 2:
					aq.id(R.id.textViewSmall).text(tf.getLinks()[childPosition].getIndex()+"::"+mContext.getText(R.string.clarify_hd));
					break;
				case 3:
					aq.id(R.id.textViewSmall).text(tf.getLinks()[childPosition].getIndex()+"::"+mContext.getText(R.string.clarify_uhd));
					break;
				case 0:
				default:
					aq.id(R.id.textViewSmall).text(tf.getLinks()[childPosition].getIndex()+"::"+mContext.getText(R.string.clarify_none));
					break;
				}				
			}
		}
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		if (null != vodHistory.get(groupPosition).getLinkRecord())
			return 0;
		
		if (null == vodHistory.get(groupPosition).getTorrentRecord())
			return 0;

		if (null == vodHistory.get(groupPosition).getTorrentRecord().getLinks())
			return 0;
		
		return vodHistory.get(groupPosition).getTorrentRecord().getLinks().length;
	}

	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return vodHistory.get(groupPosition);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		if (null == vodHistory)
			return 0;

		return vodHistory.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
//		return vodHistory[groupPosition].hashCode();
		return groupPosition;
	}
	
	protected void setGroupImage(AQuery aq, VideoLink vl) {
		if (vl.getUrl().startsWith("ed2k")) 
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_ed2k);
		else if (vl.getUrl().startsWith("magnet")) 
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_magnet);
		else if (vl.getUrl().startsWith("thunder")) 
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_thunder);
		else if (vl.getUrl().startsWith("http")) 
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_http);
		else if (vl.getUrl().startsWith("ftp")) 
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_ftp);
		else
			aq.id(R.id.imageViewType).image(R.drawable.ic_link_video);
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item, null);
		}
		AQuery aq = new AQuery(convertView);
		if(null != vodHistory.get(groupPosition).getLinkRecord()) {
			VideoLink vl = vodHistory.get(groupPosition).getLinkRecord();
			aq.id(R.id.textViewLarge).text(vl.getName());
			setGroupImage(aq, vl);
			
			int count = 0;
			if (null != vl.getM3u8Link())
				count = vl.getM3u8Link().length;
			switch(count) {
			case 1:
				aq.id(R.id.textViewSmall).text(R.string.clarify_sd);
				break;
			case 2:
				aq.id(R.id.textViewSmall).text(R.string.clarify_hd);
				break;
			case 3:
				aq.id(R.id.textViewSmall).text(R.string.clarify_uhd);
				break;
			case 0:
			default:
				aq.id(R.id.textViewSmall).text(R.string.clarify_none);
				break;
			}
		} 
		else {
			TorrentFile tf = vodHistory.get(groupPosition).getTorrentRecord();
			if (isExpanded)
				aq.id(R.id.imageViewType).image(R.drawable.ic_folder_opened);
//			aq.id(R.id.imageViewType).image(R.drawable.ic_torrent_open);
			else
				aq.id(R.id.imageViewType).image(R.drawable.ic_folder_closed);
//			aq.id(R.id.imageViewType).image(R.drawable.ic_torrent_close);
			if (null != tf.getFileName())
				aq.id(R.id.textViewLarge).text(tf.getFileName());
			else
				aq.id(R.id.textViewLarge).text(tf.getFilePathName());
			aq.id(R.id.textViewSmall).text(tf.getInfoHash());				

		}
		
		return convertView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
}
